import { clone, findTree, isArray, isNumber, isString } from 'xe-utils'
import { useUserInfo } from '@/stores/userInfo'
import { _MoneyFormat, intToMoney, toFormatTime } from '@/utils/index'
import { dayjs, ElMessage } from 'element-plus'
import { useBaseApi } from '@/api/base'
import type { LazyLoad } from 'element-plus/es/components/cascader-panel/src/node'
import type {
  DicType,
  BusinessUIAction,
  BusinessUIColumnType,
  BusinessUIQueryColumnType,
  BusinessUITableType,
  FormComponentProp,
  FormComponentType,
  BusinessUIFormType
} from '@/types/custom'

const { CustomRequest } = useBaseApi()

// 审批组件
export const approvalStatusList = [
  { label: '执行中', value: 0 },
  { label: '已完成', value: 1 },
  { label: '中止', value: 2 },
  { label: '回退', value: 3 },
  { label: '审批未通过', value: 4 },
  { label: '流程异常', value: 99 }
]

// 流程颜色
export const wfStatusTypes: TypedAny<string> = {
  0: 'primary',
  1: 'success',
  2: 'warning',
  3: 'warning',
  4: 'danger',
  5: 'info',
  99: 'danger'
}
// // 流程文字
// export const wfStatusNames: TypedAny<string> = {
//   0: '执行中',
//   1: '已完成',
//   2: '中止',
//   3: '回退',
//   4: '审批未通过',
//   5: '详情',
//   99: '流程异常'
// }

// 默认栅格布局
export const defaultCol = { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 }

// 查询栏排除的参数
// export const queryExcludeKeys = ['page', 'pageSize']

// 表格格式化 discard=true代表已废弃
export const formatOptions = [
  { label: '禁用/否', value: 'disabled' },
  { label: '是/否', value: 'boolean' },
  { label: '数字', value: 'amount', type: 'number' },
  { label: '金额转大写（中文大写）', value: 'amount-capitalize', type: 'number' },

  { label: '金额(千分位)', value: 'money-accounting', type: 'number', discard: true },
  { label: '金额(千分位)隐藏0', value: 'money-accounting-not', type: 'number', discard: true },
  { label: '金额(￥符号)', value: 'money-zhy', type: 'number', discard: true },
  { label: '金额(￥符号)隐藏0', value: 'money-zhy-not', type: 'number', discard: true },
  { label: '金额(元符号)', value: 'money-zh', type: 'number', discard: true },
  { label: '金额(元符号)隐藏0', value: 'money-zh-not', type: 'number', discard: true },

  { label: '日期格式(年)', value: 'year' },
  { label: '日期格式(年月)', value: 'month' },
  { label: '日期格式(年月日)', value: 'date' },
  { label: '时间格式(年月日时分)', value: 'dateMinute' },
  { label: '时间格式(年月日时分秒)', value: 'datetime' },
  { label: '日期格式(中文-年)', value: 'year-zh' },
  { label: '日期格式(中文-年月)', value: 'month-zh' },
  { label: '日期格式(中文-年月日)', value: 'date-zh' },
  { label: '时间格式(中文-年月日时分)', value: 'dateMinute-zh' },
  { label: '时间格式(中文-年月日时分秒)', value: 'datetime-zh' },
  { label: '时间格式(多久以前)', value: 'format-time' }
]

/**
 * 表格格式化转换
 * @param col 格式类型
 * @param value
 */
export const getFormatOptions = (
  col: BusinessUIColumnType | BusinessUIQueryColumnType,
  value: any
) => {
  const formatType = col.set_json_parse.formatType
  const formatFixed = col.set_json_parse.formatFixed || ''
  const amount = formatType?.indexOf('money') !== -1 ? Number(value) || 0 : value
  if (formatType === 'boolean') {
    return value ? '是' : ''
  } else if (formatType === 'disabled') {
    return value ? '禁用' : ''
  } else if (formatType === 'format-time' && value) {
    return toFormatTime(value)
  } else if (formatType === 'year' && value) {
    return dayjs(value).format('YYYY')
  } else if (formatType === 'month' && value) {
    return dayjs(value).format('YYYY-MM')
  } else if (formatType === 'date' && value) {
    return dayjs(value).format('YYYY-MM-DD')
  } else if (formatType === 'dateMinute' && value) {
    return dayjs(value).format('YYYY-MM-DD HH:mm')
  } else if (formatType === 'datetime' && value) {
    return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
  } else if (formatType === 'year-zh' && value) {
    return dayjs(value).format('YYYY年')
  } else if (formatType === 'month-zh' && value) {
    return dayjs(value).format('YYYY年MM月')
  } else if (formatType === 'date-zh' && value) {
    return dayjs(value).format('YYYY年MM月DD日')
  } else if (formatType === 'dateMinute-zh' && value) {
    return dayjs(value).format('YYYY年MM月DD日 HH:mm')
  } else if (formatType === 'datetime-zh' && value) {
    return dayjs(value).format('YYYY年MM月DD日 HH:mm:ss')
  } else if (formatType === 'amount') {
    if (amount) {
      return amount.toFixed(formatFixed || 0)
    } else {
      return amount
    }
  } else if (formatType === 'amount-capitalize') {
    return intToMoney(amount || 0)
  } else if (formatType === 'money-accounting' && isNumber(value)) {
    return _MoneyFormat(amount, {
      style: 'currency',
      // 输出人民币
      currency: 'CNY',
      // 输出美元，语言设置为'en'
      // currency: 'USD',
      // currencySign选项启用记帐格式
      currencySign: 'accounting',
      // 保留两位小数
      minimumFractionDigits: formatFixed || 2,
      maximumFractionDigits: formatFixed || 2
    })
  } else if (formatType === 'money-accounting-not' && isNumber(value)) {
    return value
      ? _MoneyFormat(amount, {
        style: 'currency',
        // 输出人民币
        currency: 'CNY',
        // 输出美元，语言设置为'en'
        // currency: 'USD',
        // currencySign选项启用记帐格式
        currencySign: 'accounting',
        // 保留两位小数
        minimumFractionDigits: formatFixed || 2,
        maximumFractionDigits: formatFixed || 2
      })
      : ''
  } else if (formatType === 'money-zhy' && isNumber(value)) {
    return `¥${amount.toFixed(formatFixed || 2)}`
  } else if (formatType === 'money-zhy-not' && isNumber(value)) {
    return value ? `¥${amount.toFixed(formatFixed || 2)}` : ''
  } else if (formatType === 'money-zh' && isNumber(value)) {
    return `${amount.toFixed(formatFixed || 2)}元`
  } else if (formatType === 'money-zh-not' && isNumber(value)) {
    return value ? `${amount.toFixed(formatFixed || 2)}元` : ''
  } else {
    return value
  }
}

// 表格行内组件
export const formatRowComponents = [
  {
    label: '标签（el-tag）',
    value: 'el-tag',
    document: 'https://element-plus.org/zh-CN/component/tag.html#tag-api',
    props: {}
  }
]

// 默认参数规则
export const formatValues = [
  // 时间
  { label: '今天', value: '__today__' },
  { label: '本年', value: '__this_year__' },
  { label: '去年', value: '__last_year__' },
  { label: '本月', value: '__this_month__' },
  { label: '上个月', value: '__last_month__' },

  { label: '上个月第一天', value: '__last_month_start__' },
  { label: '上个月最后一天', value: '__last_month_end__' },

  // 用户
  { label: '我', value: '__me__' },
  { label: '我的部门', value: '__me_dep__' },
  // ...
]

/**
 * 获取默认参数值
 * @param value
 * @param defaultValue
 */
export const getFormatValues = (value: string, defaultValue?: string) => {
  if (value === '__today__') {
    return dayjs().format('YYYY-MM-DD HH:mm:ss')
  } else if (value === '__this_year__') {
    return dayjs().format('YYYY')
  } else if (value === '__last_year__') {
    return dayjs().subtract(1, 'year').format('YYYY-MM')
  } else if (value === '__this_month__') {
    return dayjs().format('YYYY-MM')
  } else if (value === '__last_month__') {
    return dayjs().subtract(1, 'month').format('YYYY-MM')
  } else if (value === '__me__') {
    const { userInfos } = useUserInfo()
    return userInfos.id
  } else if (value === '__me_dep__') {
    const { userInfos } = useUserInfo()
    return userInfos.dep_id
  } else if (value === '__last_month_start__') {
    const now = dayjs()
    const lastMonth = now.subtract(1, 'month')
    const firstDayOfLastMonth = lastMonth.startOf('month')
    return firstDayOfLastMonth.format('YYYY-MM-DD')
  } else if (value === '__last_month_end__') {
    const now = dayjs()
    const lastMonth = now.subtract(1, 'month')
    const firstDayOfLastMonth = lastMonth.endOf('month')
    return firstDayOfLastMonth.format('YYYY-MM-DD')
  } else {
    return defaultValue || ''
  }
}

/**
 * 获取当前表格配置的主要信息，用于显示，提示，标题等
 * @param columns
 * @param row
 */
export const getColumnTips = (columns: BusinessUIColumnType[], row?: TypedAny) => {
  const cols = columns.filter((f) => f.set_json_parse.isTips)
  if (row && cols.length) {
    return cols.map((v) => String(row[v.field] || '')).join('，')
  } else return ''
  // const option: TypedAny = row || {}
  // return getNotEmpty(option, ['name', 'dep_name', 'app_user_name', 'id'])
}

/**
 * 字典格式化
 * @param row 行
 * @param dicTable 字典
 * @param disabledKeys 禁用字典格式化的字段
 */
export const formatDicTable = (
  row: TypedAny,
  dicTable: DicType[],
  disabledKeys?: (string | undefined)[]
) => {
  const disabled = disabledKeys || []
  const keys = Object.keys(row)
  keys.forEach((key) => {
    if (!disabled.includes(key)) {
      const value = row[key]
      const dicItems = dicTable.filter((f) => f.column_name === key)
      if (dicItems.length) {
        if (isArray(value)) {
          row[key] = dicItems
            .filter((v) => value.some((s) => s == v.value_text))
            .map((v) => v.display_text)
            .join(',')
        } else {
          const dic = dicItems.find((v) => value == v.value_text)

          if (dic) {
            if (dic.display_type && dic.display_type !== 'default') {
              row[`${key}_display_type`] = dic.display_type
            }
            row[key] = dic.display_text
          }
        }
      }
      // else if (isBoolean(value)) {
      //   row[key] = value ? '是' : '否'
      // }
    }
  })
}

/**
 * 表格格式化字典
 * @param list
 * @param dicTable
 * @param uiTable
 * @param uiForm
 */
export const formatColumnRows = (
  list: any[],
  dicTable: DicType[],
  uiTable?: BusinessUITableType,
  uiForm?: BusinessUIFormType
) => {
  list.forEach((row) => {
    const oldRow = clone(row, true)
    if (uiTable) {
      uiTable.columns.forEach((col) => {
        const value = oldRow[col.field]
        // 其他
        const formatType = col.set_json_parse.formatType
        if (formatType) {
          row[col.field] = getFormatOptions(col, value)
        }
        if (col.set_json_parse.nodeApp) {
          const status: string = getNotEmpty(row, ['wf_stauts', 'status'])
          row[`${col.field}_display_type`] = getNotEmpty(wfStatusTypes, [status], 'primary')
        }
      })
    }
    if (uiForm) {
      uiForm.items.forEach((col) => {
        const value = oldRow[col.field]
        // 其他
        const formatType = col.set_json_parse.formatType
        if (formatType) {
          row[col.field] = getFormatOptions(col, value)
        }
      })
    }
    formatDicTable(row, dicTable)
  })
}

/**
 * 获取不为空的值
 * @param row
 * @param keys // 按keys顺序获取直到有为止
 * @param defaultValue // 没有获取到的默认值(选填)
 */
export const getNotEmpty = (row: TypedAny, keys: string[], defaultValue?: any) => {
  for (let i = 0; i < keys.length; i++) {
    const value = row[keys[i]]
    if (value !== undefined) return value
  }
  return defaultValue
}

/**
 * label 名称
 * value 组件key (不允许重复)
 * elComponent element组件名称
 * document element plus文档
 * props 默认参数
 * col 响应式
 * keys match 可能使用的字段（填入会自动匹配，在创建表单时省事） ['org_id']
 * suffix string[] 结尾匹配
 * server { api: '接口地址', method: '请求方法', value: '对应id', label: '对应name', sort: '排序', data: '接口返回数据（若返回{items:[],total}则填写"items"否这不填）' }
 */
export const formComponents: FormComponentType[] = [
  {
    label: '通用组件',
    value: 'element-plus',
    document: '',
    props: {},
    col: {},
    children: [
      {
        title: '名称',
        label: '输入框（el-input）',
        value: 'el-input',
        elComponent: 'el-input',
        document: 'https://element-plus.org/zh-CN/component/input.html#select-attributes',
        props: {
          type: 'text',
          placeholder: '请输入',
          clearable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        keys: ['name'],
        suffix: ['_name']
      },
      {
        title: '备注',
        label: '多行文本输入框（el-input-textarea）',
        value: 'el-input-textarea',
        elComponent: 'el-input',
        document: 'https://element-plus.org/zh-CN/component/input.html#select-attributes',
        props: {
          type: 'textarea',
          placeholder: '请输入',
          clearable: 'true',
          rows: '5',
          autosize: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: [],
        keys: ['remark'],
        suffix: ['_remark']
      },
      {
        title: '数字框',
        label: '数字框（el-input-number）',
        value: 'el-input-number',
        elComponent: 'el-input-number',
        document: 'https://element-plus.org/zh-CN/component/input-number.html#attributes',
        props: {},
        type: 'number',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        keys: ['amt', 'price', 'days', 'seq'],
        suffix: ['_count', '_amt', '_price', '_day', '_amount', '_seq']
      },
      {
        title: '日期',
        label: '日期选择器（el-date-picker）',
        value: 'el-date-picker',
        elComponent: 'el-date-picker',
        document: 'https://element-plus.org/zh-CN/component/date-picker.html#select-attributes',
        props: {
          type: 'date',
          placeholder: '请选择',
          valueFormat: 'YYYY-MM-DD',
          editable: 'false'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        keys: ['date_begin', 'date_end', 'created_on', 'modified_on'],
        suffix: ['_month', '_date', '_year', '_time']
      },
      {
        label: '单选器（el-radio-group）',
        value: 'el-radio-group',
        elComponent: 'el-radio-group',
        document: 'https://element-plus.org/zh-CN/component/radio.html#radio-api',
        props: {},
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '字典数字分段器（el-segmented）',
        value: 'el-segmented',
        elComponent: 'el-segmented',
        document: 'https://element-plus.org/zh-CN/component/segmented.html',
        props: {},
        type: 'number',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '多选器（el-checkbox-group）',
        value: 'el-checkbox-group',
        elComponent: 'el-checkbox-group',
        document: 'https://element-plus.org/zh-CN/component/checkbox.html#checkbox-api',
        props: {},
        type: 'array',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '开关（el-switch）',
        value: 'el-switch',
        elComponent: 'el-switch',
        document: 'https://element-plus.org/zh-CN/component/switch.html#select-attributes',
        props: {
          inlinePrompt: 'true',
          activeText: '开',
          inactiveText: '关'
        },
        type: 'boolean',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '按钮（el-button）',
        value: 'el-button',
        elComponent: 'el-button',
        document: 'https://element-plus.org/zh-CN/component/button.html#button-api',
        props: {
          type: 'default'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '取色器（el-color-picker）',
        value: 'el-color-picker',
        elComponent: 'el-color-picker',
        document: 'https://element-plus.org/zh-CN/component/color-picker.html#attributes',
        props: {},
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '评分器（el-rate）',
        value: 'el-rate',
        elComponent: 'el-rate',
        document: 'https://element-plus.org/zh-CN/component/rate.html#attributes',
        props: {},
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '数字滑块（el-slider）',
        value: 'el-slider',
        elComponent: 'el-slider',
        document: 'https://element-plus.org/zh-CN/component/slider.html#api',
        props: {},
        type: 'number',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '字典下拉选择器（el-select）',
        value: 'el-select',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择',
          clearable: 'true',
          filterable: 'false'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        title: '下拉选择器',
        label: '下拉选择器（api-select）',
        value: 'api-select',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择下拉选择器',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
        // // api
        // server: {
        //   api: '/api-mis/Contract/contract_part_b_select',
        //   method: 'get',
        //   value: 'id',
        //   label: 'name'
        // }
      },
      {
        title: '级联选择器',
        label: '级联选择器（api-cascader）',
        value: 'api-cascader',
        elComponent: 'el-cascader',
        document: 'https://element-plus.org/zh-CN/component/cascader.html',
        props: {
          placeholder: '请选择',
          clearable: 'true',
          lazy: 'true',
          emitPath: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
        // // api
        // server: {
        //   api: '/api-mis/Contract/contract_part_b_select',
        //   method: 'get',
        //   value: 'id',
        //   label: 'name'
        // }
      },

      {
        title: '树形选择器',
        label: '树形选择器（api-tree-select）',
        value: 'api-tree-select',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '选择树形选择器',
          clearable: 'true',
          filterable: 'true',
          collapseTags: 'true',
          checkStrictly: 'true',
          collapseTagsTooltip: 'true',
          defaultExpandAll: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
        // // api
        // server: {
        //   api: '/api-mis/AccountBase/finance_list',
        //   method: 'get',
        //   pid: 'pid',
        //   value: 'id',
        //   label: 'name'
        // }
      }
    ]
  },
  {
    label: '提示组件',
    value: 'alert',
    document: '',
    props: {},
    col: {},
    children: [
      {
        label: '提示（el-alert）',
        value: 'el-alert',
        elComponent: 'el-alert',
        document: 'https://element-plus.org/zh-CN/component/alert.html#alert-api',
        props: {
          type: 'info',
          closable: 'false',
          showIcon: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        label: '标题栏（custom-title）',
        value: 'custom-title',
        document: 'https://element-plus.org/zh-CN/component/text.html#api',
        props: {
          'content-position': 'left',
          'border-style': 'dotted'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        label: '接口提示（api-alert）',
        value: 'api-alert',
        elComponent: 'el-alert',
        document: 'https://element-plus.org/zh-CN/component/alert.html#alert-api',
        props: {
          type: 'success'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        label: '文本显示（el-text）',
        value: 'el-text',
        elComponent: 'el-text',
        document: 'https://element-plus.org/zh-CN/component/text.html#api',
        props: {
          type: 'info'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        label: '多行文本显示（custom-pre）',
        value: 'custom-pre',
        document: '',
        props: {
          type: 'info'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        label: 'JSON显示器（custom-json）',
        value: 'custom-json',
        document: '',
        props: {},
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      }
    ]
  },
  {
    label: '系统组件',
    value: 'system',
    document: '',
    props: {},
    col: {},
    children: [
      {
        title: '公司',
        label: '公司选择（sys-corp）',
        value: 'sys-corp',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '请选择公司',
          multiple: 'false',
          clearable: 'true',
          filterable: 'false',
          collapseTags: 'true',
          checkStrictly: 'true',
          collapseTagsTooltip: 'true',
          defaultExpandAll: 'true'
        },
        type: 'array',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-base/Sub/list',
          method: 'get',
          pid: 'pid',
          value: 'id',
          label: 'name'
        },
        keys: ['org_id']
      },
      {
        title: '部门',
        label: '部门选择（sys-dep）',
        value: 'sys-dep',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '请选择部门',
          multiple: 'false',
          clearable: 'true',
          filterable: 'true',
          collapseTags: 'true',
          checkStrictly: 'true',
          collapseTagsTooltip: 'true',
          defaultExpandAll: 'true'
        },
        type: 'array',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-base/Dep/list',
          method: 'get',
          pid: 'pid',
          sort: 'sort',
          value: 'id',
          label: 'name'
        },
        keys: ['dep_id']
      },
      {
        title: '用户',
        label: '用户选择器（sys-user）',
        value: 'sys-user',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '选择或搜索用户',
          clearable: 'true',
          filterable: 'true',
          defaultExpandAll: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // keys: ['own_user', 'app_user_id', 'pay_user_id', 'borrow_user_id', 'repay_user_id'],
        suffix: ['_user', '_user_id', 'user_id', '_man']
      },
      {
        title: '岗位',
        label: '岗位选择（sys-pos）',
        value: 'sys-pos',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索岗位',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-base/Position/query',
          method: 'post',
          value: 'id',
          label: 'position_name,dep_name',
          params: {
            name: '',
            dep_id: ''
          }
        },
        keys: ['position_id']
      },
      {
        title: '省级',
        label: '省级选择（sys-prov）',
        value: 'sys-prov',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索省',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-base/Geo/provs',
          method: 'get',
          value: 'id',
          label: 'name'
        },
        keys: ['prov', 'prov_id']
      },
      {
        title: '市级',
        label: '市级选择（sys-city）',
        value: 'sys-city',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索市级',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-base/Geo/citys',
          method: 'get',
          value: 'id',
          label: 'name',
          params: {
            prov_id: ''
          }
        },
        keys: ['city', 'city_id']
      },
      {
        title: '性别',
        label: '性别选择器（sys-sex）',
        value: 'sys-sex',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '性别选择或搜索',
          clearable: 'true'
        },
        type: 'number',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        keys: ['sex'],
        options: [
          { value: 0, label: '未知' },
          { value: 1, label: '男' },
          { value: 2, label: '女' }
        ]
      },
      {
        title: '工作流',
        label: '工作流选择（sys-wf）',
        value: 'sys-wf',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索工作流',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-wf/WorkFlowDesign/list',
          method: 'get',
          value: 'id',
          label: 'name'
        }
      },
      {
        title: '工作流实体',
        label: '工作流实体选择（sys-ins）',
        value: 'sys-ins',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索工作流实体',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: []
      },
      {
        title: '审批状态',
        label: '审批状态选择器（sys-wf-status）',
        value: 'sys-wf-status',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '选择或搜索审批状态',
          clearable: 'true',
          multiple: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        keys: ['wf_stauts']
      },
      {
        title: '附件',
        label: '上传附件（custom-annex）',
        value: 'custom-annex',
        elComponent: '',
        document: '',
        props: {
          multiple: 'true'
        },
        type: 'array',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: [],
        keys: ['files']
      },
      {
        title: '图片',
        label: '上传图片（custom-image）',
        value: 'custom-image',
        elComponent: '',
        document: '',
        props: {
          multiple: 'true'
        },
        type: 'array',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: [],
        keys: ['images', 'imgs']
      },
      {
        title: '业务选择器（表格）',
        label: '业务选择器（表格）（custom-selector）',
        value: 'custom-selector',
        elComponent: '',
        document: '',
        props: {},
        type: 'array',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        title: '关联业务（表格）',
        label: '关联业务（表格）（custom-business）',
        value: 'custom-business',
        elComponent: '',
        document: '',
        props: {},
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      },
      {
        title: '关联业务（表单）',
        label: '关联业务（表单）（custom-business-form）',
        value: 'custom-business-form',
        elComponent: '',
        document: '',
        props: {},
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      }
    ]
  },
  {
    label: '查询栏专属',
    value: 'query',
    document: '',
    props: {},
    col: {},
    children: [
      {
        label: '标签页切换器（el-radio-button）',
        value: 'el-radio-button',
        elComponent: 'el-radio-group',
        document: 'https://element-plus.org/zh-CN/component/radio.html#radiogroup-api',
        props: {
          placeholder: '请选择',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24 },
        children: []
      }
    ]
  },

  // MIS组件库
  {
    label: 'MIS组件',
    value: 'mis',
    document: '',
    props: {},
    col: {},
    show: ['mis', 'wmmis'],
    hide: true,
    children: [
      {
        title: 'MIS项目',
        label: 'MIS项目选择器（mis-item）',
        value: 'mis-item',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择项目',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/Item/list_item',
          method: 'get',
          value: 'id',
          label: 'name'
        },
        keys: ['item_id']
      },
      {
        title: '销售合同',
        label: '销售合同选择器（mis-sale-contract）',
        value: 'mis-sale-contract',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择合同',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/Contract/contracts_slaes',
          method: 'get',
          value: 'id',
          label: 'name'
        },
        keys: ['contracts_id']
      },
      {
        title: '厂商',
        label: '厂商选择器（mis-mill）',
        value: 'mis-mill',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择厂商',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/AccountMember/supplier_list',
          method: 'get',
          value: 'id',
          label: 'name',
          data: ''
        }
      },
      {
        title: '客户',
        label: '客户选择器（mis-client）',
        value: 'mis-client',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择客户',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/AccountMember/customer_list',
          method: 'get',
          value: 'id',
          label: 'name'
        }
      },
      {
        title: '合同甲方部门',
        label: '合同甲方部门选择器（mis-part-a）',
        value: 'mis-part-a',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择合同甲方部门',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/Contract/contract_part_a_deps',
          method: 'get',
          value: 'id',
          label: 'name'
        }
      },
      {
        title: '合同乙方单位',
        label: '合同乙方单位选择器（mis-part-b）',
        value: 'mis-part-b',
        elComponent: 'el-select',
        document: 'https://element-plus.org/zh-CN/component/select.html#select-api',
        props: {
          placeholder: '请选择合同乙方单位',
          clearable: 'true',
          filterable: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/Contract/contract_part_b_select',
          method: 'get',
          value: 'id',
          label: 'name'
        }
      },
      {
        title: '会计科目',
        label: '会计科目选择器（mis-account）',
        value: 'mis-account',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '选择会计科目',
          clearable: 'true',
          filterable: 'true',
          collapseTags: 'true',
          checkStrictly: 'true',
          collapseTagsTooltip: 'true',
          defaultExpandAll: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/AccountBase/finance_list',
          method: 'get',
          pid: 'pid',
          value: 'id',
          label: 'name'
        }
      },
      {
        title: '文档分类',
        label: '文档分类选择器（mis-doc）',
        value: 'mis-doc',
        elComponent: 'el-tree-select',
        document: 'https://element-plus.org/zh-CN/component/tree.html#attributes',
        props: {
          placeholder: '请选择文档分类',
          clearable: 'true',
          filterable: 'true',
          defaultExpandAll: 'true'
        },
        type: 'string',
        col: { xs: 24, sm: 12, md: 6, lg: 6, xl: 4 },
        children: [],
        // api
        server: {
          api: '/api-mis/OADoc/cats',
          method: 'get',
          pid: 'p_id',
          value: 'id',
          label: 'name'
        },
        keys: ['doc_id']
      }
    ]
  }
]

/**
 * 表格默认参数
 */
export const defaultColumns = [
  { field: 'name', title: '名称', props: { width: '200', align: 'left' } },
  { field: 'title', title: '标题', props: { width: '200', align: 'left' } },
  { field: 'remark', title: '备注', props: { width: '400', align: 'left' } },
  { field: 'content', title: '内容', props: { width: '300', align: 'left' } },

  { field: 'ins_name', title: '实体名' },
  { field: 'bank_name', title: '银行名' },
  { field: 'bank_number', title: '银行卡号' },
  { field: 'address', title: '地址' },
  { field: 'descript', title: '描述' },
  { field: 'phone', title: '手机号' },
  { field: 'p_name', title: '父级名称' },
  { field: 'idx', title: '#', props: { width: '40' } },
  { field: 'seq', title: '#', props: { width: '40' } },

  { field: 'category', title: '分类' },
  { field: 'business', title: '业务' },
  { field: 'days', title: '天' },

  { field: 'flag', title: '标志' },
  { field: 'ver', title: '版本' },
  { field: 'code', title: '编码' },
  { field: 'cat_name', title: '分类' },
  { field: 'org_name', title: '公司' },
  { field: 'dep_name', title: '部门' },
  { field: 'own_user_name', title: '用户' },
  { field: 'item_name', title: '项目' },
  { field: 'id', title: 'ID' },
  { field: 'pid', title: '父级ID' },
  { field: 'files', title: '附件' },
  { field: 'create_user_name', title: '经办人' },
  { field: 'app_user_name', title: '发布人' },
  { field: 'actions', title: '操作' },
  { field: 'pay_mode', title: '付款方式' },

  { field: 'status_name', title: '审批状态', props: { width: '150' }, nodeApp: true },
  { field: 'wf_status_name', title: '审批状态', props: { width: '150' }, nodeApp: true },
  { field: 'wf_node', title: '当前状态', props: { width: '150' }, nodeApp: true },

  { field: 'amt', title: '金额', props: { width: '100', align: 'right' }, formatType: 'money-zhy' },
  {
    field: 'app_money',
    title: '借款金额',
    props: { width: '100', align: 'right' },
    formatType: 'money-zhy'
  },

  { field: 'app_date', title: '申请日期', formatType: 'date' },
  { field: 'begin_date', title: '开始日期' },
  { field: 'end_date', title: '结束日期' },
  { field: 'repay_date', title: '还款日期' },
  { field: 'demand_plan_use_date', title: '计划使用日期', formatType: 'date' },
  { field: 'created_on', title: '创建时间', props: { width: '150' }, formatType: 'dateMinute' },
  { field: 'modified_on', title: '修改时间', props: { width: '150' }, formatType: 'dateMinute' }
]

/**
 * 获取属性
 * @param form
 * @param key elComponent
 * cascadeProps cascade组建的内嵌props参数
 */
const cascadeProps = [
  'expandTrigger',
  'multiple',
  'checkStrictly',
  'emitPath',
  'lazy',
  'disabled',
  'leaf',
  'hoverThreshold'
]
const elComponent = ['el-cascader']
export const getBusinessBind = (form: BusinessUIQueryColumnType, key?: string) => {
  const props = form.set_json_parse.props
  for (const k in props) {
    if (props[k] === 'true' || props[k] === 'false') {
      props[k] = props[k] === 'true'
    } else if (k === 'defaultTime' && props[k]) {
      if (isString(props[k])) props[k] = new Date(dayjs().format('YYYY-MM-DD ') + props[k])
    }
    const prop = formComponentProps.find((f) => f.value === k)
    if (prop && prop.type === 'number') props[k] = Number(props[k])

    const elComponentKey = key || form.elComponent || ''

    if (elComponent.includes(elComponentKey) && cascadeProps.includes(k)) {
      if (props['props']) {
        props['props'][k] = props[k]
      } else {
        props['props'] = { [k]: props[k] }
      }

      if (props.props.lazy && !props.props['lazyLoad']) {
        const lazyLoad: LazyLoad = (node, resolve) => {
          const { set_json_parse } = form
          const { url, method } = set_json_parse

          if (!url) {
            ElMessage.warning(`${form.title}组件未配置接口`)
            return
          }

          const { level, value } = node

          const params = { id: value, level }

          CustomRequest({
            url,
            method,
            params,
            data: method === 'post' ? params : undefined
          })
            .then(({ data }) => {
              if (isArray(data)) {
                resolve(
                  data.map((v) => {
                    return {
                      value: v.id,
                      label: v.name,
                      leaf: v.leaf
                    }
                  })
                )
              } else {
                resolve([])
              }
            })
            .catch(() => {
              resolve([])
            })
        }
        props.props['lazyLoad'] = lazyLoad
      }

      delete props[k]
    }
  }
  return props
}

/**
 * vxe-grid columns属性
 */
export const vxeGridColumnProps: FormComponentProp[] = [
  {
    label: '类型（seq序号,checkbox复选框,radio单选,action操作,expand展开行,html格式化）',
    value: 'type',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['seq', 'checkbox', 'radio', 'action', 'expand', 'html']
      }
    ]
  },
  {
    label: '树形列（用于展开按钮在哪个列显示，通常于名称下）',
    value: 'treeNode',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '最小列宽度；会自动将剩余空间按比例分配',
    value: 'minWidth',
    type: 'number/string'
  },
  {
    label: '最大列宽度；会自动将剩余空间按比例分配',
    value: 'maxWidth',
    type: 'number/string'
  },
  {
    label: '列是否允许拖动列宽调整大小',
    value: 'resizable',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '列固定',
    value: 'fixed',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['left', 'right']
      }
    ]
  },
  {
    label: '是否显示',
    value: 'visible',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '对齐方式 left（左对齐）, center（居中对齐）, right（右对齐）',
    value: 'align',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['left', 'center', 'right']
      }
    ]
  },
  {
    label: '表头对齐方式 left（左对齐）, center（居中对齐）, right（右对齐）',
    value: 'headerAlign',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['left', 'center', 'right']
      }
    ]
  },
  {
    label: '表尾对齐方式 left（左对齐）, center（居中对齐）, right（右对齐）',
    value: 'footerAlign',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['left', 'center', 'right']
      }
    ]
  },
  {
    label: '超出显示省略号',
    value: 'showOverflow',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '表头超出显示省略号',
    value: 'showHeaderOverflow',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '数据排序，是否允许列排序',
    value: 'sortable',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '额外的参数（可以用来存放一些私有参数）',
    value: 'params',
    type: 'any'
  },
  {
    label: '是否严格的遵循父子不互相关联的做法（只对type=radio|checkbox生效）',
    value: 'checkStrictly',
    type: 'boolean',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '表格自定义class',
    value: 'className',
    type: 'string',
    values: [
      {
        title: '值',
        values: [
          'color-primary',
          'color-success',
          'color-warning',
          'color-danger',
          'color-info',
          'color-red',
          'color-green',
          'bg-primary',
          'bg-success',
          'bg-warning',
          'bg-danger',
          'bg-info',
          'bg-red',
          'bg-green',
          'font-bold',
          'font{1-100}'
        ]
      }
    ]
  }
]

/**
 * El 表单组件的传值说明
 */
export const formComponentProps: FormComponentProp[] = [
  {
    label: '框中是否显示选中值的完整路径',
    value: 'showAllLevels',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否显示icon',
    value: 'showIcon',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否可以关闭',
    value: 'closable',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '说明信息',
    value: 'description',
    type: 'string'
  },
  {
    label: '最大输入长度',
    value: 'maxlength',
    type: 'string/number'
  },
  {
    label: '最小输入长度',
    value: 'minlength',
    type: 'string/number'
  },
  {
    label: '是否显示统计字数, 只在 type 为 "text"或 "textarea" 的时候生效',
    value: 'show-word-limit',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '标题组件的border样式',
    value: 'border-style',
    type: 'string',
    default_value: 'solid',
    values: [
      {
        title: '值',
        values: [
          'none',
          'solid',
          'hidden',
          'dashed',
          'dotted',
          'double',
          'groove',
          'inset',
          'outset'
        ]
      }
    ]
  },
  {
    label: '标题组件的对齐方式',
    value: 'content-position',
    type: 'string',
    default_value: 'left',
    values: [
      {
        title: '值',
        values: ['left', 'right', 'center']
      }
    ]
  },
  {
    label: '文本框是否自适应高度，只在 type 为 "textarea" 的时候生效',
    value: 'autosize',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '输入框占位文本',
    value: 'placeholder',
    type: 'string'
  },
  {
    label: '时间开始文本',
    value: 'start-placeholder',
    type: 'string'
  },
  {
    label: '时间结束文本',
    value: 'end-placeholder',
    type: 'string'
  },
  {
    label: '是否显示清除按钮，只在 type 不为 "textarea" 时有效',
    value: 'clearable',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否显示切换密码图标',
    value: 'show-password',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否禁用',
    value: 'disabled',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '时间选择器，文本框可输入',
    value: 'editable',
    type: 'boolean',
    default_value: 'false',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '输入框尺寸，只在 type 不为 "textarea" 时有效',
    value: 'size',
    type: 'large/default/small',
    values: [
      {
        title: '值',
        values: ['large', 'default', 'small']
      }
    ]
  },
  {
    label: '自定义前缀图标',
    value: 'prefix-icon',
    type: 'string'
  },
  {
    label:
      '在选中节点改变时，是否返回由该节点所在的各级菜单的值所组成的数组，若设置 false，则只返回该节点的值',
    value: 'emitPath',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '输入框中是否显示选中值的完整路径',
    value: 'showAllLevels',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '自定义后缀图标',
    value: 'suffix-icon',
    type: 'string'
  },
  {
    label: '输入框行数，仅 type 为 "textarea" 时有效',
    value: 'rows',
    type: 'number'
  },
  {
    label: 'Input Number数值精度',
    value: 'precision',
    type: 'number'
  },
  {
    label: "textarea 高度是否自适应，仅 type 为 'textarea' 时生效。",
    value: 'autosize',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: 'textarea 原生 autocomplete 属性',
    value: 'autocomplete',
    type: 'string'
  },
  {
    label: '等价于原生 input name 属性',
    value: 'name',
    type: 'string'
  },
  {
    label: '是否只读',
    value: 'readonly',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '设置最大值',
    value: 'max',
    type: 'number'
  },
  {
    label: '设置最小值',
    value: 'min',
    type: 'number'
  },
  {
    label: '设置最小值',
    value: 'min',
    type: 'number'
  },
  {
    label: '原生属性，设置输入字段的合法数字间隔',
    value: 'step',
    type: 'number'
  },
  {
    label: '控制是否能被用户缩放',
    value: 'resize',
    type: 'none/both/horizontal/vertical',
    values: [
      {
        title: '值',
        values: ['none', 'both', 'horizontal', 'vertical']
      }
    ]
  },
  {
    label: '原生属性，自动获取焦点',
    value: 'autofocus',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '等价于原生 input aria-label 属性',
    value: 'label',
    type: 'string'
  },
  {
    label: '次级菜单的展开方式',
    value: 'expandTrigger',
    type: 'click/hover',
    values: [
      {
        title: '值',
        values: ['click', 'hover']
      }
    ]
  },
  {
    label: '是否多选',
    value: 'multiple',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否严格的遵守父子节点不互相关联',
    value: 'checkStrictly',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否动态加载子节点，需与lazyLoad方法结合使用',
    value: 'lazy',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '指定选项的值为选项对象的某个属性值',
    value: 'value',
    type: 'string'
  },
  {
    label: '指定选项标签为选项对象的某个属性值',
    value: 'label',
    type: 'string'
  },
  {
    label: '指定选项的子选项为选项对象的某个属性值',
    value: 'children',
    type: 'string'
  },
  {
    label: '指定选项的禁用',
    value: 'disabled',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '指定选项的叶子节点的标志位为选项对象的某个属性值',
    value: 'leaf',
    type: 'string'
  },
  {
    label: 'hover 时展开菜单的灵敏度阈值',
    value: 'hoverThreshold',
    type: 'number'
  },
  {
    label: '选择日期后的默认时间值。 如未指定则默认时间值为 00:00:00',
    value: 'defaultTime',
    type: 'string',
    values: [
      {
        title: '值',
        values: ['08:00', '08:30', '09:00', '17:30', '18:00', '18:30']
      }
    ]
  },
  {
    label: '显示在输入框中的格式 YYYY-MM-DD HH:mm:ss',
    value: 'format',
    type: 'string',
    default_value: 'YYYY-MM-DD HH:mm:ss',
    values: [
      {
        title: '值',
        values: [
          'YYYY',
          'YYYY-MM',
          'YYYY-MM-DD',
          'YYYY-MM-DD HH',
          'YYYY-MM-DD HH:mm',
          'YYYY-MM-DD HH:mm:ss'
        ]
      }
    ]
  },
  {
    label: '得到值的格式 YYYY-MM-DD HH:mm:ss',
    value: 'valueFormat',
    type: 'string',
    default_value: 'YYYY-MM-DD HH:mm:ss',
    values: [
      {
        title: '值',
        values: [
          'YYYY',
          'YYYY-MM',
          'YYYY-MM-DD',
          'YYYY-MM-DD HH',
          'YYYY-MM-DD HH:mm',
          'YYYY-MM-DD HH:mm:ss'
        ]
      }
    ]
  },
  {
    label: '显示类型',
    value: 'type',
    type: 'string',
    values: [
      {
        title: '日期',
        values: [
          'year',
          'month',
          'date',
          'dates',
          'datetime',
          'week',
          'datetimerange',
          'daterange',
          'monthrange'
        ]
      },
      {
        title: '其他',
        values: ['success', 'warning', 'primary', 'danger', 'info', 'text']
      }
    ]
  },
  {
    label: '是否将date-picker的下拉列表插入至body元素',
    value: 'teleported',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '显示复选框',
    value: 'show-checkbox',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '是否默认展开所有节点',
    value: 'defaultExpandAll',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: 'css样式（示例：“height:500px;”）',
    value: 'style',
    type: 'string',
    default_value: ''
  },
  {
    label: '显示省略号（el-text专属）',
    value: 'truncated',
    type: 'boolean',
    default_value: 'true',
    values: [
      {
        title: '值',
        values: ['true', 'false']
      }
    ]
  },
  {
    label: '最大行数（el-text专属）',
    value: 'line-clamp',
    type: 'string',
    default_value: '2'
  },
  {
    label: '自定义元素标签（el-text专属）',
    value: 'tag',
    type: 'string',
    default_value: 'span'
  }
]

/**
 * 获取表单组件属性
 * @param items
 * @param callback
 */
export const setFormComponents = (
  items: BusinessUIQueryColumnType[],
  callback?: (item: BusinessUIQueryColumnType) => void
) => {
  items.forEach((v) => {
    const elComp = findTree(formComponents, (item) => item.value === v.component)
    if (elComp && elComp.item) {
      v.elComponent = elComp.item.elComponent
      v.options = elComp.item.options
      v.type = elComp.item.type
    }
    if (callback) callback(v)
  })
}

/**
 * 默认操作按钮
 */
export const defaultActions: BusinessUIAction[] = [
  {
    action: 'Add',
    name: '新增',
    global: true,
    visible: true,
    idx: 0,
    api: '',
    method: '',
    component: '',
    type: 'success',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '业务表单的新增',
    defaultValue: true
  },
  {
    action: 'Insert',
    name: '插入一行',
    global: true,
    visible: true,
    idx: 1,
    api: '',
    method: '',
    component: '',
    type: 'success',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '表格行内编辑，插入一行数据',
    defaultValue: false
  },
  {
    action: 'BatchSave',
    name: '保存编辑',
    global: true,
    visible: true,
    idx: 2,
    api: '',
    method: '',
    component: '',
    type: 'primary',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '表格行内编辑的批量保存按钮',
    defaultValue: false
  },
  {
    action: 'BatchDelete',
    name: '移除勾选',
    global: true,
    visible: true,
    idx: 3,
    api: '',
    method: '',
    component: '',
    type: 'danger',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '表格行内编辑的批量移除勾选',
    defaultValue: false
  },
  {
    action: 'Udp',
    name: '编辑',
    global: false,
    visible: true,
    idx: 4,
    api: '',
    method: '',
    component: '',
    type: 'primary',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '业务表单的编辑',
    defaultValue: true
  },
  {
    action: 'Del',
    name: '删除',
    global: false,
    visible: true,
    idx: 5,
    api: '',
    method: '',
    component: '',
    type: 'danger',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '业务表单的删除',
    defaultValue: true
  },
  {
    action: 'SetUser',
    name: '配置用户',
    global: false,
    visible: true,
    idx: 6,
    api: '',
    method: '',
    component: '',
    type: 'default',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '专属配置用户'
  },
  {
    action: 'RPwd',
    name: '重置密码',
    global: false,
    visible: true,
    idx: 7,
    api: '/api-base/User/reset_pwd',
    method: 'put',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '用户专属重置密码',
    model: 'request'
  },
  {
    action: 'can_submit',
    name: '提交',
    global: false,
    visible: true,
    idx: 8,
    api: '',
    method: '',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '流程图专属按钮"提交"',
    defaultValue: true
  },
  {
    action: 'can_un_submit',
    name: '撤回',
    global: false,
    visible: true,
    idx: 9,
    api: '',
    method: '',
    component: '',
    type: 'danger',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '流程图专属按钮"撤回"',
    defaultValue: true
  },
  {
    action: 'Deduction',
    name: '冲抵借款',
    global: true,
    visible: true,
    idx: 10,
    api: '',
    method: '',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '冲抵借款专属新增按钮'
  },
  {
    action: 'Overtime',
    name: '冲抵加班',
    global: true,
    visible: true,
    idx: 11,
    api: '',
    method: '',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '冲抵加班专属新增按钮'
  },
  {
    action: 'ExportJson',
    name: '导出JSON',
    global: true,
    visible: true,
    idx: 12,
    api: '',
    method: '',
    component: '',
    type: 'info',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    model: 'trigger',
    set_json: '',
    set_json_parse: {},
    description: '将勾选数据导出为JSON'
  },
  {
    action: 'ImportJson',
    name: '导入JSON',
    global: true,
    visible: true,
    idx: 13,
    api: '',
    method: '',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    model: 'trigger',
    set_json: '',
    set_json_parse: {},
    description: '将JSON数据导入系统'
  },
  {
    action: 'ImportExcel',
    name: '导入Excel',
    global: true,
    visible: true,
    idx: 14,
    api: '',
    method: '',
    component: '',
    type: 'warning',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    model: '',
    set_json: '',
    set_json_parse: {},
    description: '将Excel数据导入系统'
  },
  {
    action: 'ExportWord',
    name: '导出Word',
    global: false,
    visible: true,
    idx: 15,
    api: '',
    method: '',
    component: '',
    type: 'primary',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    model: 'download',
    set_json: '',
    set_json_parse: {},
    description: '将详情转为Word'
  },
  {
    action: 'Print',
    name: '预览打印',
    global: true,
    visible: true,
    idx: 16,
    api: '',
    method: '',
    component: '',
    type: 'default',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    set_json: '',
    set_json_parse: {},
    description: '定制各业务的打印（配置到顶部变成批量勾选打印）'
  },
  {
    action: 'TableSelector',
    name: '表格选择器',
    global: true,
    visible: true,
    idx: 17,
    api: '',
    method: '',
    component: '',
    type: 'default',
    default_value: '',
    default_value_parse: {},
    business_id: '',
    model: 'selector',
    set_json: '',
    set_json_parse: {},
    description: '让业务的表格变成选择器'
  }
]
